package org.apache.servicecomb.samples.practise.houserush.user.center.api;

import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;

import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.HouseOrderApi;
import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.RealestateApi;
import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.po.*;
import org.apache.servicecomb.samples.practise.houserush.user.center.rpc.HouseApi;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RestSchema(schemaId = "userCenterApiTest")
@RequestMapping("/")
public class UserCenterRestApiImpl implements UserCenterApi {

    @RpcReference(microserviceName = "realestate", schemaId = "realestateApiTest")
    private RealestateApi realestateApi;

    @RpcReference(microserviceName = "house-order", schemaId = "houseOrderApiRest")
    private HouseOrderApi houseOrderApi;

    @RpcReference(microserviceName = "realestate", schemaId = "houseApi")
    private HouseApi houseApi;


    @GetMapping("testRPC")
    @Override
    public String testRPC() {
        String s = "现在时间 ：" + realestateApi.testProvider();
        return s;
    }

    //    GET 查询客户收藏列表 /favorites
    @GetMapping("favorites")
    public List<Favorite> findMyFavorite(@RequestHeader int customerId) {
        List<Favorite> myFavorite = houseOrderApi.findMyFavorite(customerId);
        for (Favorite favorite : myFavorite) {
            HouseOrder houseOrder = houseOrderApi.findOne(favorite.getHouseOrderId());
            House house = houseApi.findHouse(houseOrder.getHouseId());
            favorite.setHouseName(house.getName());
            favorite.setPrice(house.getPrice());
            favorite.setBuilDingName(house.getBuilding().getName());
            favorite.setRealestateName(house.getBuilding().getRealestate().getName());
            favorite.setState(houseOrder.getState());
        }
        return myFavorite;
    }

    @GetMapping("favorites/{houseId}")
    public HouseDetail findByHouseIdDetail(@PathVariable("houseId") int houseId) {
        HouseDetail houseDetail = new HouseDetail();
        House house = houseApi.findHouse(houseId);
        houseDetail.setHouseName(house.getName());
        houseDetail.setPrice(house.getPrice());
        houseDetail.setBuilDingName(house.getBuilding().getName());

        Realestate realestate = house.getBuilding().getRealestate();
        houseDetail.setRealestateName(realestate.getName());
        houseDetail.setAddress(realestate.getAddress());
        houseDetail.setType(realestate.getType());
        houseDetail.setAvgprice(realestate.getAvgprice());
        houseDetail.setUseyear(realestate.getUseyear());
        houseDetail.setUsernum(realestate.getUsernum());
        houseDetail.setArea(realestate.getArea());
        houseDetail.setBuildname(realestate.getBuildname());

        HouseOrder houseOrder = houseOrderApi.findAllByHouseId(houseId);
        houseDetail.setHouseOrderId(houseOrder.getId());
        houseDetail.setState(houseOrder.getState());
        return houseDetail;
    }

    @GetMapping("buyHouseOrderState")
    public List<HouseOrder> findMyHouseOrder(@RequestHeader int customerId) {
        List<HouseOrder> list = houseOrderApi.findAllByCustomerId(customerId);
        for (HouseOrder houseOrder : list) {
            Integer houseId = houseOrder.getHouseId();
            House house = houseApi.findHouse(houseId);
            houseOrder.setHouseName(house.getName());
            houseOrder.setPrice(house.getPrice());
            houseOrder.setBuilDingName(house.getBuilding().getName());
            houseOrder.setRealestateName(house.getBuilding().getRealestate().getName());
        }
        return list;
    }
}
